
var contentTV;
var contentTVLS;
var contentTVLSNG;
var contentTVSH;
var contentTVTCB;
var contentTVTT;

var contentDV;
var contentDVLSNG;
var contentDVSH;
var contentDVTCB;
var contentDVCN;
var checkContentTV = 0; // check da lay the div cua thuc vat chua
var checkContentTVLS = 0; // check da lay the div cua thuc vat lam san chua
var checkContentTVLSNG = 0;
var checkContentTVSH_TCB = 0;
var checkContentTVTT = 0;
var checkContentDV = 0; // check da lay the div cua dong vat chua
var checkContentDVLSNG = 0;
var tvid;  // id thuc vat
var dvid;  // id thuc vat
var valiChoiceTV; // bien luu ca the thuc vat nao dang duoc xem thong tin
var valiChoiceTVLatOld;
var valiChoiceTVLongOld;
var valiChoiceDV; // bien luu ca the dong vat nao dang duoc xem thong tin
var valiChoiceDVLatOld;
var valiChoiceDVLongOld;
// hien thi cac thong tin cua thuc dong vat duoc chon
function showMapInfoBubbleMessage(markerWithLabel, loaitdv, name, latatude, longatude){
	infoBubble = new InfoBubble({
        maxWidth: 640
      });
	// hien thi thong tin thuc vat chung
	if(loaitdv.substring(0,2) == "tv") {
		var i = 1;
		while(i <= dataTV.infoTVHST.length && dataTV.infoTVHST[i][2] != name) i++;
	    if(checkContentTV == 0) { contentTV = document.getElementById('infoBubbleThongTinTV'); checkContentTV++; }
	    
	    contentTV.querySelector('input[id="tvloaikh"]').value = dataTV.infoTVHST[i][1];
		contentTV.querySelector('input[id="tvloaidp"]').value = dataTV.infoTVHST[i][2];
		contentTV.querySelector('input[id="tvloailt"]').value = dataTV.infoTVHST[i][3];
		contentTV.querySelector('input[id="tvhokh"]').value = dataTV.infoTVHST[i][4];
		contentTV.querySelector('input[id="tvhodp"]').value = dataTV.infoTVHST[i][5];
		contentTV.querySelector('input[id="tvholt"]').value = dataTV.infoTVHST[i][6];
		contentTV.querySelector('input[id="tvgtgo"]').checked = false;
		contentTV.querySelector('input[id="tvgtcanh"]').checked = false;
		contentTV.querySelector('input[id="tvgtthuoc"]').checked = false;
		contentTV.querySelector('input[id="tvgtvlxaydung"]').checked = false;
		contentTV.querySelector('input[id="tvgtthucongmynghe"]').checked = false;
		contentTV.querySelector('input[id="tvgtanduoc"]').checked = false;
		contentTV.querySelector('input[id="tvgtcaydoc"]').checked = false;
		contentTV.querySelector('input[id="tvgtchotinhdau"]').checked = false;
		contentTV.querySelector('input[id="tvgtnhua"]').checked = false;
		contentTV.querySelector('input[id="tvgtsoi"]').checked = false;
		contentTV.querySelector('input[id="tvgthuocnhuom"]').checked = false;
		contentTV.querySelector('input[id="tvgtcongdungkhac"]').checked = false;
		var ktgt = 0;
		for(var j = 0; j<dataTV.infoTVHST[i][7].length; j++){
			while(j < dataTV.infoTVHST[i][7].length && dataTV.infoTVHST[i][7].charAt(j) != ",") j++;
			var giatri = dataTV.infoTVHST[i][7].substring(ktgt, j);
			ktgt = j + 1;
			if(giatri == "Gỗ")contentTV.querySelector('input[id="tvgtgo"]').checked = true;
			if(giatri == "Cảnh")contentTV.querySelector('input[id="tvgtcanh"]').checked = true;
			if(giatri == "Thuốc")contentTV.querySelector('input[id="tvgtthuoc"]').checked = true;
			if(giatri == "Vật liệu xây dựng")contentTV.querySelector('input[id="tvgtvlxaydung"]').checked = true;
			if(giatri == "Thủ công mỹ nghệ")contentTV.querySelector('input[id="tvgtthucongmynghe"]').checked = true;
			if(giatri == "Ăn được")contentTV.querySelector('input[id="tvgtanduoc"]').checked = true;
			if(giatri == "Cây độc")contentTV.querySelector('input[id="tvgtcaydoc"]').checked = true;
			if(giatri == "Cho tinh dầu")contentTV.querySelector('input[id="tvgtchotinhdau"]').checked = true;
			if(giatri == "Nhựa")contentTV.querySelector('input[id="tvgtnhua"]').checked = true;
			if(giatri == "Sợi")contentTV.querySelector('input[id="tvgtsoi"]').checked = true;
			if(giatri == "Thuốc nhuộm")contentTV.querySelector('input[id="tvgthuocnhuom"]').checked = true;
			if(giatri == "Công dụng khác")contentTV.querySelector('input[id="tvgtcongdungkhac"]').checked = true;
		}
		contentTV.querySelector('textarea[id="tvmota"]').value = dataTV.infoTVHST[i][8];		
		if(dataTV.infoTVHST[i][9] == "Nhanh") contentTV.querySelector('select[id="sltvloaisinhtruong"]').options[0].selected = true;
		else if(dataTV.infoTVHST[i][9] == "Trung bình") contentTV.querySelector('select[id="sltvloaisinhtruong"]').options[1].selected = true;
		else if(dataTV.infoTVHST[i][9] == "Chậm") contentTV.querySelector('select[id="sltvloaisinhtruong"]').options[2].selected = true;
		
		contentTV.style.display = '';
	    
	    infoBubble.addTab('Thông tin chung', contentTV);
	    // kiểm tra xem đang click loài thực vật nào
	    if(loaitdv == "tvLamSan"){
	    	i = 1;
			while(i <= dataTV.infoTVHST.length && dataTV.infoTVHST[i][2] != name) i++;
			var j = 1;
			while(j <= dataTV.infoTVLS.length && dataTV.infoTVHST[i][0] != dataTV.infoTVLS[j][1]) j++;
			if(checkContentTVLS == 0) { contentTVLS = document.getElementById('infoBubbleTVLamSan'); checkContentTVLS++; }
			
		    //hien thi thong tin thuc vat LAM SAN		    
		    contentTVLS.querySelector('input[id="tvchieucao"]').value = dataTV.infoTVLS[j][2].toFixed(2);
		    contentTVLS.querySelector('input[id="tvdotuoi"]').value = dataTV.infoTVLS[j][8].toFixed(1);
			contentTVLS.querySelector('input[id="tvnamkhaosat"]').value = dataTV.infoTVLS[j][9];
			if("Cây tái sinh" == dataTV.infoTVLS[j][4]) contentTVLS.querySelector('select[id="tvtang"]').options[0].selected = true;
			else if("Cây bụi" == dataTV.infoTVLS[j][4]) contentTVLS.querySelector('select[id="tvtang"]').options[1].selected = true;
			else if("Dưới tán" == dataTV.infoTVLS[j][4]) contentTVLS.querySelector('select[id="tvtang"]').options[2].selected = true;
			else if("Tán" == dataTV.infoTVLS[j][4]) contentTVLS.querySelector('select[id="tvtang"]').options[3].selected = true;
			else if("Trên tán" == dataTV.infoTVLS[j][4]) contentTVLS.querySelector('select[id="tvtang"]').options[4].selected = true;
			contentTVLS.querySelector('input[id="tvdongtay"]').value = dataTV.infoTVLS[j][6].toFixed(1);
			contentTVLS.querySelector('input[id="tvnambac"]').value = dataTV.infoTVLS[j][7].toFixed(1);
			if("Gỗ lớn" == dataTV.infoTVLS[j][5]) contentTVLS.querySelector('select[id="tvdangsong"]').options[0].selected = true;
			else if("Gỗ trung bình" == dataTV.infoTVLS[j][5]) contentTVLS.querySelector('select[id="tvdangsong"]').options[1].selected = true;
			else if("Gỗ nhỏ" == dataTV.infoTVLS[j][5]) contentTVLS.querySelector('select[id="tvdangsong"]').options[2].selected = true;
			contentTVLS.querySelector('input[id="tvlatatude"]').value = latatude;
			contentTVLS.querySelector('input[id="tvlongatude"]').value = longatude;
			if("Nhỏ hơn 10cm" == dataTV.infoTVLS[j][3]) contentTVLS.querySelector('select[id="tvduongkinh"]').options[0].selected = true;
			else if("Lớn trung bình" == dataTV.infoTVLS[j][3]) contentTVLS.querySelector('select[id="tvduongkinh"]').options[1].selected = true;
			
			var k = 1;
			while(k <= dataTV.pointTVLS.length && dataTV.infoTVLS[j][0] != dataTV.pointTVLS[k][1]) k++;
			contentTVLS.querySelector('input[id="tvbankinhks"]').value = dataTV.pointTVLS[k][4].toFixed(1);
			contentTVLS.querySelector('input[id="tvmatdo"]').value = dataTV.pointTVLS[k][5];
			
			contentTVLS.style.display = '';
			
		    infoBubble.addTab('Đặc trưng riêng', contentTVLS);
	    } else if(loaitdv == "tvLamSanNgoaiGo"){
	    	i = 1;
			while(i <= dataTV.infoTVHST.length && dataTV.infoTVHST[i][2] != name) i++;
			var j = 1;
			while(j <= dataTV.infoTVLSNG.length && dataTV.infoTVHST[i][0] != dataTV.infoTVLSNG[j][1]) j++;
			if(checkContentTVLSNG == 0) { contentTVLSNG = document.getElementById('infoBubbleTVLamSanNgoaiGo'); checkContentTVLSNG++; }
			
		    //hien thi thong tin thuc vat LAM SAN		    
		    contentTVLSNG.querySelector('input[id="tvchieucao"]').value = dataTV.infoTVLSNG[j][2].toFixed(2);
		    contentTVLSNG.querySelector('input[id="tvdotuoi"]').value = dataTV.infoTVLSNG[j][4].toFixed(1);
			contentTVLSNG.querySelector('input[id="tvnamkhaosat"]').value = dataTV.infoTVLSNG[j][5];
			if("Cây tái sinh" == dataTV.infoTVLSNG[j][3]) contentTVLSNG.querySelector('select[id="tvtang"]').options[0].selected = true;
			else if("Cây bụi" == dataTV.infoTVLSNG[j][3]) contentTVLSNG.querySelector('select[id="tvtang"]').options[1].selected = true;
			else if("Dưới tán" == dataTV.infoTVLSNG[j][3]) contentTVLSNG.querySelector('select[id="tvtang"]').options[2].selected = true;
			else if("Tán" == dataTV.infoTVLSNG[j][3]) contentTVLSNG.querySelector('select[id="tvtang"]').options[3].selected = true;
			else if("Trên tán" == dataTV.infoTVLSNG[j][3]) contentTVLSNG.querySelector('select[id="tvtang"]').options[4].selected = true;
			contentTVLSNG.querySelector('input[id="tvlatatude"]').value = latatude;
			contentTVLSNG.querySelector('input[id="tvlongatude"]').value = longatude;
			
			var k = 1;
			while(k <= dataTV.pointTVLSNG.length && dataTV.infoTVLSNG[j][0] != dataTV.pointTVLSNG[k][1]) k++;
			contentTVLSNG.querySelector('input[id="tvbankinhks"]').value = dataTV.pointTVLSNG[k][4].toFixed(1);
			contentTVLSNG.querySelector('input[id="tvmatdo"]').value = dataTV.pointTVLSNG[k][5];
			
			contentTVLSNG.style.display = '';
			
		    infoBubble.addTab('Đặc trưng riêng', contentTVLSNG);
	    } else if(loaitdv == "tvSanHo"){
	    	i = 1;
			while(i <= dataTV.infoTVHST.length && dataTV.infoTVHST[i][2] != name) i++;
			var j = 1;
			while(j <= dataTV.infoTVSH.length && dataTV.infoTVHST[i][0] != dataTV.infoTVSH[j][1]) j++;
			if(checkContentTVSH_TCB == 0) {contentTVSH = document.getElementById('infoBubbleTVSanHo_ThamCoBien'); checkContentTVSH_TCB++; }
			else contentTVSH = contentTVTCB;
			
		    //hien thi thong tin thuc vat SAN HO	    
		    contentTVSH.querySelector('input[id="tvchieucao"]').value = dataTV.infoTVSH[j][4].toFixed(2);
		    contentTVSH.querySelector('input[id="tvdotuoi"]').value = dataTV.infoTVSH[j][5].toFixed(1);
			contentTVSH.querySelector('input[id="tvnamkhaosat"]').value = dataTV.infoTVSH[j][6];
			contentTVSH.querySelector('input[id="tvlatatude"]').value = latatude;
			contentTVSH.querySelector('input[id="tvlongatude"]').value = longatude;
			
			var k = 1;
			while(k <= dataTV.pointTVSH.length && dataTV.infoTVSH[j][0] != dataTV.pointTVSH[k][1]) k++;
			contentTVSH.querySelector('input[id="tvbankinhks"]').value = dataTV.pointTVSH[k][4].toFixed(1);
			contentTVSH.querySelector('input[id="tvmatdo"]').value = dataTV.pointTVSH[k][5];
			
			if("Tái sinh hạt" == dataTV.infoTVSH[j][2]) contentTVSH.querySelector('select[id="tvnguongotaisinh"]').options[0].selected = true;
			else if("Tái sinh chồi" == dataTV.infoTVSH[j][2]) contentTVSH.querySelector('select[id="tvnguongotaisinh"]').options[1].selected = true;
			
			if("Tốt" == dataTV.infoTVSH[j][3]) contentTVSH.querySelector('select[id="tvchatluongsinhtruong"]').options[0].selected = true;
			else if("Trung bình" == dataTV.infoTVSH[j][3]) contentTVSH.querySelector('select[id="tvchatluongsinhtruong"]').options[1].selected = true;
			else if("Yếu" == dataTV.infoTVSH[j][3]) contentTVSH.querySelector('select[id="tvchatluongsinhtruong"]').options[2].selected = true;
			
			contentTVSH.style.display = '';
			
		    infoBubble.addTab('Đặc trưng riêng', contentTVSH);
	    } else if(loaitdv == "tvThamCoBien"){
	    	i = 1;
			while(i <= dataTV.infoTVHST.length && dataTV.infoTVHST[i][2] != name) i++;
			var j = 1;
			while(j <= dataTV.infoTVTCB.length && dataTV.infoTVHST[i][0] != dataTV.infoTVTCB[j][1]) j++;
			if(checkContentTVSH_TCB == 0) { contentTVTCB = document.getElementById('infoBubbleTVSanHo_ThamCoBien'); checkContentTVSH_TCB++; }
			else contentTVTCB = contentTVSH;
			
		    //hien thi thong tin thuc vat THAM CO BIEN		    
		    contentTVTCB.querySelector('input[id="tvchieucao"]').value = dataTV.infoTVTCB[j][4].toFixed(2);
		    contentTVTCB.querySelector('input[id="tvdotuoi"]').value = dataTV.infoTVTCB[j][5].toFixed(1);
			contentTVTCB.querySelector('input[id="tvnamkhaosat"]').value = dataTV.infoTVTCB[j][6];
			contentTVTCB.querySelector('input[id="tvlatatude"]').value = latatude;
			contentTVTCB.querySelector('input[id="tvlongatude"]').value = longatude;
			
			var k = 1;
			while(k <= dataTV.pointTVTCB.length && dataTV.infoTVTCB[j][0] != dataTV.pointTVTCB[k][1]) k++;
			contentTVTCB.querySelector('input[id="tvbankinhks"]').value = dataTV.pointTVTCB[k][4].toFixed(1);
			contentTVTCB.querySelector('input[id="tvmatdo"]').value = dataTV.pointTVTCB[k][5];
			
			if("Tái sinh hạt" == dataTV.infoTVTCB[j][2]) contentTVTCB.querySelector('select[id="tvnguongotaisinh"]').options[0].selected = true;
			else if("Tái sinh chồi" == dataTV.infoTVTCB[j][2]) contentTVTCB.querySelector('select[id="tvnguongotaisinh"]').options[1].selected = true;
			if("Tốt" == dataTV.infoTVTCB[j][3]) contentTVTCB.querySelector('select[id="tvchatluongsinhtruong"]').options[0].selected = true;
			else if("Trung bình" == dataTV.infoTVTCB[j][3]) contentTVTCB.querySelector('select[id="tvchatluongsinhtruong"]').options[1].selected = true;
			else if("Yếu" == dataTV.infoTVTCB[j][3]) contentTVTCB.querySelector('select[id="tvchatluongsinhtruong"]').options[2].selected = true;
			
			contentTVTCB.style.display = '';
			
		    infoBubble.addTab('Đặc trưng riêng', contentTVTCB);
	    } else if(loaitdv == "tvTrongTrot"){
	    	i = 1;
			while(i <= dataTV.infoTVHST.length && dataTV.infoTVHST[i][2] != name) i++;
			var j = 1;
			while(j <= dataTV.infoTVTT.length && dataTV.infoTVHST[i][0] != dataTV.infoTVTT[j][1]) j++;
			if(checkContentTVTT == 0) { contentTVTT = document.getElementById('infoBubbleTVTrongTrot'); checkContentTVTT++; }
			
		    //hien thi thong tin thuc vat TRONG TROT		    
		    contentTVTT.querySelector('input[id="tvchieucao"]').value = dataTV.infoTVTT[j][4].toFixed(2);
		    contentTVTT.querySelector('input[id="tvvumua"]').value = dataTV.infoTVTT[j][6];
			contentTVTT.querySelector('input[id="tvnamkhaosat"]').value = dataTV.infoTVTT[j][7];
			contentTVTT.querySelector('input[id="tvlatatude"]').value = latatude;
			contentTVTT.querySelector('input[id="tvlongatude"]').value = longatude;
			
			var k = 1;
			while(k <= dataTV.pointTVTT.length && dataTV.infoTVTT[j][0] != dataTV.pointTVTT[k][1]) k++;
			contentTVTT.querySelector('input[id="tvbankinhks"]').value = dataTV.pointTVTT[k][4].toFixed(1);
			contentTVTT.querySelector('input[id="tvmatdo"]').value = dataTV.pointTVTT[k][5];
			
			if("Tái sinh hạt" == dataTV.infoTVTT[j][2]) contentTVTT.querySelector('select[id="tvnguongotaisinh"]').options[0].selected = true;
			else if("Tái sinh chồi" == dataTV.infoTVTT[j][2]) contentTVTT.querySelector('select[id="tvnguongotaisinh"]').options[1].selected = true;
			if("Tốt" == dataTV.infoTVTT[j][3]) contentTVTT.querySelector('select[id="tvchatluongsinhtruong"]').options[0].selected = true;
			else if("Trung bình" == dataTV.infoTVTT[j][3]) contentTVTT.querySelector('select[id="tvchatluongsinhtruong"]').options[1].selected = true;
			else if("Yếu" == dataTV.infoTVTT[j][3]) contentTVTT.querySelector('select[id="tvchatluongsinhtruong"]').options[2].selected = true;
			
			contentTVTT.querySelector('input[id="tvnangsuat"]').value = dataTV.infoTVTT[j][5];
			
			contentTVTT.style.display = '';
			
		    infoBubble.addTab('Đặc trưng riêng', contentTVTT);
	    }

	} else if(loaitdv.substring(0,2) == "dv"){
		var i = 1;
		while(i <= dataDV.infoDVHST.length && dataDV.infoDVHST[i][6] != name) i++;
	    if(checkContentDV == 0) { contentDV = document.getElementById('infoBubbleThongTinDV'); checkContentDV++; }
	    contentDV.querySelector('input[id="dvloaikh"]').value = dataDV.infoDVHST[i][6];
	    contentDV.querySelector('input[id="dvnganhkh"]').value = dataDV.infoDVHST[i][1];
	    contentDV.querySelector('input[id="dvlopkh"]').value = dataDV.infoDVHST[i][2];
	    contentDV.querySelector('input[id="dvbokh"]').value = dataDV.infoDVHST[i][3];
	    contentDV.querySelector('input[id="dvhokh"]').value = dataDV.infoDVHST[i][4];
	    contentDV.querySelector('input[id="dvchikh"]').value = dataDV.infoDVHST[i][5];
	    
	    var j = 1;
		while(j <= dataDV.infoDVTLT.length && dataDV.infoDVHST[i][12] != dataDV.infoDVTLT[j][0]) j++;
	    contentDV.querySelector('input[id="dvloailt"]').value = dataDV.infoDVTLT[j][6];
	    contentDV.querySelector('input[id="dvnganhlt"]').value = dataDV.infoDVTLT[j][1];
	    contentDV.querySelector('input[id="dvloplt"]').value = dataDV.infoDVTLT[j][2];
	    contentDV.querySelector('input[id="dvbolt"]').value = dataDV.infoDVTLT[j][3];
	    contentDV.querySelector('input[id="dvholt"]').value = dataDV.infoDVTLT[j][4];
	    contentDV.querySelector('input[id="dvchilt"]').value = dataDV.infoDVTLT[j][5];
	    
	    if("Cả hai" == dataDV.infoDVHST[i][7]) contentDV.querySelector('select[id="dvnoisong"]').options[0].selected = true;
		else if("Trên cạn" == dataDV.infoDVHST[i][7]) contentDV.querySelector('select[id="dvnoisong"]').options[1].selected = true;
		else if("Dưới nước" == dataDV.infoDVHST[i][7]) contentDV.querySelector('select[id="dvnoisong"]').options[2].selected = true;
	    
	    if("Cả hai" == dataDV.infoDVHST[i][10]) contentDV.querySelector('select[id="dvhoatdong"]').options[0].selected = true;
		else if("Ban đêm" == dataDV.infoDVHST[i][10]) contentDV.querySelector('select[id="dvhoatdong"]').options[1].selected = true;
		else if("Ban ngày" == dataDV.infoDVHST[i][10]) contentDV.querySelector('select[id="dvhoatdong"]').options[2].selected = true;
	    
	    contentDV.querySelector('input[id="dvthucan"]').value = dataDV.infoDVHST[i][9];
	    contentDV.querySelector('textarea[id="dvmota"]').value = dataDV.infoDVHST[i][8];
	    
	    contentDV.style.display = '';
		
	    infoBubble.addTab('Thông tin chung', contentDV);
	    
	    if(loaitdv == "dvLamSanNgoaiGo"){
	    	i = 1;
			while(i <= dataDV.infoDVHST.length && dataDV.infoDVHST[i][6] != name) i++;
			var j = 1;
			while(j <= dataDV.infoDVLSNG.length && dataDV.infoDVHST[i][0] != dataDV.infoDVLSNG[j][1]) j++;
			if(checkContentDVLSNG == 0) { contentDVLSNG = document.getElementById('infoBubbleDongVatAll'); checkContentDVLSNG++; }
			
			contentDVLSNG.querySelector('input[id="dvdotuoi"]').value = dataDV.infoDVLSNG[j][3];
			contentDVLSNG.querySelector('input[id="dvnamkhaosat"]').value = dataDV.infoDVLSNG[j][2];
			
			if("Sinh con" == dataDV.infoDVLSNG[i][4]) contentDVLSNG.querySelector('select[id="dvhinhthucsinhsan"]').options[0].selected = true;
			else if("Đẻ trứng" == dataDV.infoDVLSNG[i][4]) contentDVLSNG.querySelector('select[id="dvhinhthucsinhsan"]').options[1].selected = true;
			
			contentDVLSNG.querySelector('input[id="dvlatatude"]').value = latatude;
			contentDVLSNG.querySelector('input[id="dvlongatude"]').value = longatude;
			
			var k = 1;
			while(k <= dataDV.pointDVLSNG.length && dataDV.infoDVLSNG[j][0] != dataDV.pointDVLSNG[k][1]) k++;
			contentDVLSNG.querySelector('input[id="dvbankinhks"]').value = dataDV.pointDVLSNG[k][4].toFixed(1);
			contentDVLSNG.querySelector('input[id="dvmatdo"]').value = dataDV.pointDVLSNG[k][5];
			
			contentDVLSNG.style.display = '';
			infoBubble.addTab('Đặc trưng riêng', contentDVLSNG);
	    }
	} 
	
	infoBubble.open(map, markerWithLabel);
	
}